﻿
@{
    ViewBag.Title = "发送";

    Newtonsoft.Json.Linq.JArray jArray = (Newtonsoft.Json.Linq.JArray)ViewData["json"];
}
<br />
@foreach (Newtonsoft.Json.Linq.JObject jObject in jArray)
{
    <div style="padding-left:10px; margin:5px 0;">
        <input type="checkbox" name="step" value="@(jObject.Value<string>("id"))" style="vertical-align:middle;"/>
        <label style="vertical-align:middle;">@(jObject.Value<string>("name"))</label>
        <input type="hidden" id="step_users_@(jObject.Value<string>("id"))" value="@(!string.IsNullOrWhiteSpace(jObject.Value<string>("users"))?jObject.Value<string>("users"):"u_EB03262C-AB60-4BC6-A4C0-96E66A4229FE")" />
    </div>
}
<div style="margin:5px 0; text-align:center;">
    <input type="button" value="确定" onclick="ok();" />
</div>
<script>
    var isNext = @jArray.Count > 0;//是否还有后续步骤
    $(function () {
        //如果没有后续步骤，则直接完成
        if (!isNext) {
            executeFlow([], "completed");
        }
    });
    function ok()
    {
        if ($(":checked[name='step']").size() == 0) {
            alert("没有选择要发送的步骤!");
            return;
        }
        var steps = new Array();
        $(":checked[name='step']").each(function () {
            steps.push({ "id": $(this).val(), "users": $("#step_users_" + $(this).val()).val(), "completedtime": "" });
        });
        executeFlow(steps, "submit");
    }

    function executeFlow(steps, type) {
        var data = { "flowid": "c540f280-5e40-4746-a64e-8cf70071c766", "type": type, "steps": JSON.stringify(steps) };
        $.ajax({
            url: "ExecuteFlow@(Html.Raw(Request.Url.Query))", data: data, type: "post", dataType:"json", success: function (json) {
                if (json.errcode != 0) {
                    alert(json.errmsg);
                }
                else {
                    var data = json.data;
                    alert(data.Messages);
                    if (json.data.IsSuccess) {
                        opener.location = '/Home/WaitTasks';
                        window.close();
                    }
                }
            }
        });
    }
</script>
